package com.haulmont.asu.web.ui.gantt;

import com.haulmont.asu.entity.Stage;
import com.haulmont.asu.entity.Step;

import java.util.Date;
import java.util.List;

/**
 * Author: Sergey42
 * Date: 22.12.13 19:54
 */
public class StageAdapter implements Item {

    Stage stage;

    StageAdapter(Stage stage) {
        this.stage = stage;
    }

    @Override
    public String getName() {
        return stage.getName();
    }

    @Override
    public Date factStart() {

        List<Step> steps = stage.getSteps();

        Date minDate = null;

        for (Step step : steps) {
            StepAdapter sa = new StepAdapter(step);

            if (sa.factStart() != null) {
                if (minDate == null || sa.factStart().before(minDate)) {
                    minDate = sa.factStart();
                }
            }
        }

        return minDate;
    }

    @Override
    public Date factFinish() {
        List<Step> steps = stage.getSteps();

        Date maxDate = null;

        for (Step step : steps) {
            StepAdapter sa = new StepAdapter(step);

            if (sa.factFinish() != null) {
                if (maxDate == null || sa.factFinish().after(maxDate)) {
                    maxDate = sa.factFinish();
                }
            }
        }

        return maxDate;
    }

    @Override
    public Date planStart() {
        List<Step> steps = stage.getSteps();

        Date minDate = null;

        for (Step step : steps) {
            StepAdapter sa = new StepAdapter(step);

            if (sa.planStart() != null) {
                if (minDate == null || sa.planStart().before(minDate)) {
                    minDate = sa.planStart();
                }
            }
        }

        return minDate;
    }

    @Override
    public Date planFinish() {
        List<Step> steps = stage.getSteps();

        Date maxDate = null;

        for (Step step : steps) {
            StepAdapter sa = new StepAdapter(step);

            if (sa.planFinish() != null) {
                if (maxDate == null || sa.planFinish().after(maxDate)) {
                    maxDate = sa.planFinish();
                }
            }
        }

        return maxDate;
    }
}
